<?php
/**
 * Created by Dumitru Russu
 * User: developer
 * Date: 9/17/12
 * Time: 9:30 AM
 * To change this template use File | Settings | File Templates.
 */
class Page_Be_Products_PhotoGalleries extends Page {

    /**
     * @var Db_Products_PhotosGalleries
     */
    protected $tableObject;

    function __construct() {

        $this->idTableObject = get('id', 'int');
		$this->classTableObject = 'Db_Products_PhotosGalleries';
		$this->actionsTableObject = array(
			'photosAction'
		);
        parent::__construct();
    }

    public function defaultAction() {
        self::$TPL->appendJsFile('admin/photo_galleries');
        self::$TPL->setBodyTemplate('list_photo_galleries');
        return array(
            'type' => 'display'
        );
    }

    public function photosAction() {
		self::$TPL->appendCssFile('admin/photos');
		self::$TPL->appendJsFile('admin/photos');
		$galleryName = (!empty($this->tableObject->objectArray->pg_name) ?  $this->tableObject->objectArray->pg_name  : t(T_PHOTO_GALLERIES_TITLE, 'no_name'));
        $this->addBreadcrumb(t(T_PAGE_TITLE, 'photos') . ' "' . $galleryName .'"', '?action=photos&id=' . $this->idTableObject, true);
        self::$TPL->setBodyTemplate('list_photos');
        return array(
            'type' => 'display'
        );
    }

	public function ajaxLoadPhotosAction() {
		$allColumns = array('nr', 'ph_resized', 'ph_name', 'ph_ow', 'ph_oh');
		$searchColumns = array('ph_resized', 'ph_name', 'ph_ow', 'ph_oh');
		list($sql, $orderBy) = Utils::getSqlQueriesForPagination($allColumns, $searchColumns);

		$limit = '';
		if (post('iDisplayLength', 'int') > 0) {
			$limit = "LIMIT ". post('iDisplayStart', 'int') .', '. post('iDisplayLength', 'int');
		}

		$photos = Db_Products_PhotosGalleries::getPhotosByGalleryId($this->idTableObject, $sql, $orderBy, $limit);
		$totalElements = Db_Products_PhotosGalleries::getTotalItems($this->idTableObject);
		$totalFilteredElements = Db_Products_PhotosGalleries::getTotalItems($this->idTableObject, $sql);

		self::$TPL->assign('photos', $photos);
		self::$TPL->assign('totalElements', $totalElements);
		self::$TPL->assign('totalFilteredElements', $totalFilteredElements);

		return array(
				'type' => 'encoded_json',
				'content' => self::$TPL->display('json_list_photos', true)
		);

	}

	public function ajaxLoadPhotoGalleriesAction() {
		$allColumns = array('nr', 'pg_date', 'pg_gallery_type', 'mem_ident', 'action');
		$searchColumns = array('pg_date', 'pg_gallery_type', 'mem_ident');
		list($searchQuery, $orderBy, $limit) = Utils::getSqlQueriesForPagination($allColumns, $searchColumns, "Db_Products_PhotosGalleries", 'getTotalElements' );
		if($searchQuery !== false) {
			// Paging
			$photoGalleriesList = Db_Products_PhotosGalleries::loadAllPhotoGalleries($limit, $searchQuery, $orderBy);
			self::$TPL->assign('photoGalleriesList',$photoGalleriesList);
		}
		return array(
			'type' => 'encoded_json',
			'content' => self::$TPL->display('json_list_photo_galleries', true)
		);

	}

	public function ajaxGetAllGalleryTypeAction() {
		return array(
			'type' => 'json',
			'content' => Db_Products_PhotosGalleries::getEnumValues('pg_gallery_type')
		);
	}


}

